
class StringTool {
  //替换字符中的空格和换行标签
  static String textFlutterString(String _text) {
    if (_text != null) {
      _text = _text.replaceAll(RegExp(r'<br\/?>'), "\r\n");
      _text = _text.replaceAll(RegExp(r'&nbsp;'), " ");
    } else {
      _text = '';
    }
    return _text;
  }

  //驼峰转化为下划线
  static String? camel2under(String val) {
    if (val == null) {
      return null;
    }
    String separator = "_";
    val = val.replaceAllMapped(RegExp(r"([a-z])([A-Z])"), (Match m) {
      return "${m[1]! + separator + m[2]!}";
    }).toLowerCase();
    return val;
  }

  //下划线转化为驼峰
  static String? under2camel(String val) {
    if (val == null) {
      return null;
    }
    String underString = "";
    val = val.toLowerCase().replaceAll(new RegExp(r'_'), " ");
    List<String> sarr = val.split(" ");
    for (int i = 0; i < sarr.length; i++) {
      String w;
      if (i > 0) {
        w = sarr[i].substring(0, 1).toUpperCase() + sarr[i].substring(1);
      } else {
        w = sarr[i];
      }

      underString += w;
    }
    return underString;
  }
  //移除url的域名前缀
  static String removeURLPrefix(String val){
    if(val == null) {
      return '';
    }
    val = val.replaceAll(new RegExp(r'^((http:\/\/)|(https:\/\/))?([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}'), "");
    return val;
  }

  static void long_p(String msg) {
    //因为String的length是字符数量不是字节数量所以为了防止中文字符过多，
    //  把4*1024的MAX字节打印长度改为1000字符数
    int maxStrLength = 1000;
    //大于1000时
    while (msg.length > maxStrLength) {
      print(msg.substring(0, maxStrLength));
      msg = msg.substring(maxStrLength);
    }
    //剩余部分
    print(msg);
  }

  static String getDuration(int mseconds) {
    int seconds = mseconds~/1000;
    int hour = 0;
    int minute = 0;
    int second = 0;
    String timeStr;
    if (seconds == null) {
      return "00:00";
    }
    if (seconds <= 0) {
      return "00:00";
    } else {
      hour = (seconds / 3600).floor();
      int time = seconds % 3600;
      minute = (time / 60).floor();
      second = time % 60;
      if (hour <= 0) {
        timeStr =  "${unitFormat(minute)}:${unitFormat(second)}";
      } else {
        timeStr = "${unitFormat(hour)}:${unitFormat(minute)}:${unitFormat(second)}";
      }
    }
    return timeStr;
  }

  static String getDurationStr(int mseconds) {
    int seconds = mseconds~/1000;
    int hour = 0;
    int minute = 0;
    int second = 0;
    String timeStr;
    if (seconds == null) {
      return "0${'小时'}:0${'分钟'}";
    }
    if (seconds <= 0) {
      return "0${'小时'}:0${'分钟'}";
    } else {
      hour = (seconds / 3600).floor();
      int time = seconds % 3600;
      minute = (time / 60).floor();
      second = time % 60;
      if (hour <= 0) {
        timeStr =  "$minute${'分钟'}";
      } else {
        timeStr = "$hour${'小时'}:$minute${'分钟'}";
      }
    }
    return timeStr;
  }

  static String unitFormat(int i) {
    String retStr;
    if (i >= 0 && i < 10) {
      retStr = "0$i";
    } else {
      retStr = "$i";
    }
    return retStr;
  }

}